package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteGoodsOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.GoodsDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.GoodsCommand;
import com.cskaoyan.wordmemorize.request.GoodsPageRequest;
import com.cskaoyan.wordmemorize.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;
    //添加商品
   @PostMapping("")
    public Result addGoods(@RequestBody GoodsCommand goods){
       goodsService.addGoods(goods);
       return Result.ok("添加商品成功");

   }

    //修改商品
    @PutMapping("")
    public Result updateGoods(@RequestBody GoodsCommand goods){
        goodsService.updateGoods(goods);
        return Result.ok("修改商品成功");
    }
    //修改商品状态
    @PutMapping("status/{goodsId}")
    public Result updateGoodsStatus(@PathVariable("goodsId") Long goodsId, @RequestParam Integer status){
        goodsService.updateGoodsStatus(goodsId,status);
        return Result.ok("修改商品状态成功");
    }
    //获取商品分页列表
    @GetMapping("list")
    public Result<PageDTO<GoodsDTO>> getGoodsPage(GoodsPageRequest request){
        PageDTO<GoodsDTO> goodsPage = goodsService.getGoodsPage(request);
        return Result.ok(goodsPage);
    }
    //删除商品
    @DeleteMapping("{goodsId}")
    public Result deleteGoods(@PathVariable("goodsId") Long goodsId){
        DeleteGoodsOperateDTO deleteGoodsOperateDTO = goodsService.deleteGoods(goodsId);
        return Result.ok(deleteGoodsOperateDTO);
    }
    //删除选中商品
    @DeleteMapping("selected")
    public Result deleteGoodsSelected(@RequestBody BatchDeleteCommand ids){
       List<DeleteGoodsOperateDTO> deleteGoodsOperateDTOS = goodsService.deleteGoodsSelected(ids);
       return Result.ok(deleteGoodsOperateDTOS);

    }
}
